import { POST } from '../../utils/Request'
import {inisHelper} from '../../utils/helper';

export const user = {
    namespaced: true,
    state : {
        isLogin:0,
        Info:{},
        token:''
    },
    mutations: {
        setStatus(state, info) {
            state.isLogin = info
          },
        setUserInfo(state,info){
            state.user = info
        },
        setToken(state,info){
            state.token = info
        }
    },
    actions:{
        async getUserInfo({commit}){
            let s = sessionStorage.getItem('user-info')  || "{}"
            if (s == undefined || s == "{}" || s == ""){
                let resp = await POST("/user/getUserInfo");
                if (resp.code === 1){
                    localStorage.setItem('isLogin',1)
                    commit("setUserInfo",resp.data.userInfo)
                    sessionStorage.setItem('user-info',JSON.stringify(resp.data.userInfo))
                }else {
                    commit('setStatus',0)
                    sessionStorage.removeItem('user-info')
                }
            }else {
                commit('setUserInfo',JSON.parse(s))
                commit('setStatus',1)
            }
        },
        setToken({commit},token){
            commit('setToken',token)
        }
    },
    getters:{
        status(state){
            return state.isLogin
        },
        userInfo(state){
            return state.user
        },
        token(state){
            return state.token
        }
    }
}